package cc.chinagps.gateway.unit.eg.upload.bean;

import cc.chinagps.gateway.unit.eg.util.ConvUtil;
import cc.chinagps.gateway.unit.eg.util.EGTimeFormatUtil;
import lombok.Data;
import org.seg.lib.util.Util;

@Data
public class JMCDriving {
    long eventTime;
    int eventType;
    int LocationState;
    Integer lat;
    Integer lng;
    Integer speed;

    public void parse(byte[] data, int pos) throws Exception {
        String collectTime = "20" + cc.chinagps.gateway.util.Util.getDateTime(data, pos, 6);
        eventTime = EGTimeFormatUtil.parseGMT8(collectTime).getTime();
        pos += 6;

        int serialNo = Util.getShort(data, pos);
        pos += 2;

        eventType = data[pos++];
        byte loc = data[pos++];
        if ((loc & 0x1) != 0) {
            LocationState = 1;
        } else {
            LocationState = 0;
        }
        // north east china
        boolean isSouth = false;
        boolean isWest = false;
        if ((loc & 0x2) != 0) {
            isSouth = true;
        }
        if ((loc & 0x4) != 0) {
            isWest = true;
        }
        lng = ConvUtil.fromInt(Util.getInt(data, pos), 0xFFFFFFFF);
        if (isSouth && lng != null) {
            lng = -1 * lng;
        }
        pos += 4;

        lat = ConvUtil.fromInt(Util.getInt(data, pos), 0xFFFFFFFF);
        if (isWest && lat != null) {
            lat = -1 * lat;
        }
        pos += 4;

        Double speedD = ConvUtil.getDouble(data, pos, (short) 0xFFFF, 256, 3);
        if (speedD != null) {
            speed = (int) (speedD * 1000);
        }
        pos += 2;

    }
}
